#include "ExprCreator.h"
#include "Expr.h"
#include "AddExpr.h"
#include "SubExpr.h"
#include "MulExpr.h"
#include "DivExpr.h"
#include "ConstExpr.h"
Expr* ExprCreator::CreateAddExpr(){
	Expr* expr2 = this->estack.top(); this->estack.pop();
	Expr* expr1 = this->estack.top(); this->estack.pop();
	Expr* expr = new AddExpr(expr1,expr2);
	return expr;
}
Expr* ExprCreator::CreateSubExpr(){
	Expr* expr2 = this->estack.top(); this->estack.pop();
	Expr* expr1 = this->estack.top(); this->estack.pop();
	Expr* expr = new SubExpr(expr1,expr2);
	return expr;
}
Expr* ExprCreator::CreateMulExpr(){
	Expr* expr2 = this->estack.top(); this->estack.pop();
	Expr* expr1 = this->estack.top(); this->estack.pop();
	Expr* expr = new MulExpr(expr1,expr2);
	return expr;
}
Expr* ExprCreator::CreateDivExpr(){
	Expr* expr2 = this->estack.top(); this->estack.pop();
	Expr* expr1 = this->estack.top(); this->estack.pop();
	Expr* expr = new DivExpr(expr1,expr2);
	return expr;
}
Expr* ExprCreator::CreateConstExpr(const int num){
	Expr* expr = new ConstExpr(num);
	return expr;
}
Expr* ExprCreator::CreateExpr(std::istream& in){
	char c;
	int num;
	Expr* expr = 0;
	while(in>>c,c!='='){
		if(c=='+'){
			expr = CreateAddExpr();
		}else if(c=='-'){
			expr = CreateSubExpr();
		}else if(c=='*'){
			expr = CreateMulExpr();
		}else if(c=='/'){
			expr = CreateDivExpr();
		}else{
			in.putback(c);
			in>>num;
			expr = CreateConstExpr(num);
		}
		estack.push(expr);
	}
	expr = this->estack.top(); this->estack.pop();
	return expr;
}
